package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.MatteBorder;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.forms.SiteCostUpdateFrom;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.utility.Common;
import vcp.client.utility.Common.UserNotification;
import vcp.client.utility.UtilityHelper;
import vcp.entity.Site;
import vcp.entity.SiteCostUpdates;
import vcp.enums.DocumentState;
import vcp.enums.PopUpMessageType;
import vcp.enums.SiteEnum;
import vcp.enums.SiteEnum.SiteCostsUpdateStatus;
/**
 * <p> update costs view class </p>
 * @author VCP team
 *
 */
public class UIUpdateCostsView extends UIAbstractView implements KeyListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = - 8633874569019804719L;

	private boolean isEditMode = false;
	private DocumentState documentState;

	private JButton btnEdit;
	private JButton btnSubmit;
	private JButton btnUndo;
	private JTextField tfSingleUse;
	private JTextField tfOrderAhead;
	private JTextField tfCasual;
	private JTextField tfFull;
	private JLabel lblUpdateStatus;

	private SiteEnum currentSiteOnScreen;
	/**
	 * getter
	 * @return current site on screen
	 */
	public SiteEnum getSelectedSite ()
	{
		return this.currentSiteOnScreen;
	}

	private String singleHourPrice;
	private String orderAheadHourPrice;
	private String casualPrice;
	private String fullPrice;
	private JTextField tfSiteName;

	private IFormSubmitionListener <SiteCostUpdateFrom> formSubmitionListener;
	/**
	 * setter
	 * @param formSubmitionListener
	 */
	public void setFormSubmitionListener ( IFormSubmitionListener <SiteCostUpdateFrom> formSubmitionListener )
	{
		this.formSubmitionListener = formSubmitionListener;
	}
	/**
	 * constructor
	 */
	public UIUpdateCostsView ()
	{
		super();
		this.documentState = DocumentState.NOT_EDITED;
		this.isEditMode = false;
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * method loads site costs
	 * @param workerSite
	 */
	public void loadSiteCosts ( Site workerSite )
	{
		this.currentSiteOnScreen = workerSite.getSite();
		this.singleHourPrice = String.valueOf(workerSite.getSingleUsePricePerHour());
		this.orderAheadHourPrice = String.valueOf(workerSite.getOrderAheadPricePerHour());
		this.casualPrice = String.valueOf(workerSite.getCasualPriceSingleCar());
		this.fullPrice = String.valueOf(workerSite.getFullPriceSingleCar());

		this.tfSingleUse.setText(this.singleHourPrice);
		this.tfOrderAhead.setText(this.orderAheadHourPrice);
		this.tfCasual.setText(this.casualPrice);
		this.tfFull.setText(this.fullPrice);
		this.tfSiteName.setText(this.currentSiteOnScreen.toString());
	}
	/**
	 * method loads cost change request status 
	 * @param update
	 */
	public void loadUpdateStatus ( SiteCostUpdates update )
	{
		if ( update != null )
		{
			this.lblUpdateStatus.setText(update.getStatus().toString());
			this.lblUpdateStatus.setForeground(this.getUpdateStatusColorCoded(update.getStatus()));

			Site site = new Site();
			site.setSite(update.getSiteEnum());
			site.setSingleUsePricePerHour(update.getSingleUsePricePerHour());
			site.setOrderAheadPricePerHour(update.getOrderAheadPricePerHour());
			site.setCasualPriceSingleCar(update.getCasualPriceSingleCar());
			site.setFullPriceSingleCar(update.getFullPriceSingleCar());

			this.loadSiteCosts(site);
		}
		else
		{
			this.lblUpdateStatus.setText("");
		}
		this.setCostChangeEnable(update);
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	/**
	 * method checks if prices are legal
	 * @return true if legal
	 */
	private boolean isValidPrices ()
	{
		boolean isValid = true;
		try
		{
			Float.parseFloat(this.tfSingleUse.getText());
			Float.parseFloat(this.tfOrderAhead.getText());
			Float.parseFloat(this.tfCasual.getText());
			Float.parseFloat(this.tfFull.getText());
		}
		catch ( NumberFormatException e )
		{
			isValid = false;
		}
		return isValid;
	}
	/**
	 * method enables changing costs
	 * @param update
	 */
	private void setCostChangeEnable ( SiteCostUpdates update )
	{
		this.updateManageButtons();
		if ( update != null )
		{
			if ( update.getStatus() == SiteCostsUpdateStatus.PENDING )
			{
				this.btnSubmit.setEnabled(false);
				this.btnEdit.setEnabled(false);
				this.btnUndo.setEnabled(false);
			}
		}
	}
	/**
	 * method enables  manager buttons
	 */
	private void updateManageButtons ()
	{
		this.btnSubmit.setEnabled(this.documentState == DocumentState.EDITED);
		this.btnUndo.setEnabled(this.isEditMode);
		this.btnEdit.setEnabled( ! this.isEditMode);
	}
	/**
	 * method enables editing prices
	 * @param edit
	 */
	private void canEditPrices ( boolean edit )
	{
		this.tfSingleUse.setEditable(edit);
		this.tfOrderAhead.setEditable(edit);
		this.tfCasual.setEditable(edit);
		this.tfFull.setEditable(edit);
	}
	/**
	 * getter
	 * @param status
	 * @return the color of the cost update request status
	 */
	private Color getUpdateStatusColorCoded ( SiteCostsUpdateStatus status )
	{
		Color textColor = null;
		switch ( status )
		{
			case REJECTED:
				textColor = Color.RED;
				break;
			case APPROVED:
				textColor = Color.GREEN;
				break;
			case PENDING:
				textColor = Color.YELLOW;
				break;
		}
		return textColor;
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	public void resetView ()
	{
		this.singleHourPrice = "";
		this.orderAheadHourPrice = "";
		this.casualPrice = "";
		this.fullPrice = "";

		this.tfSingleUse.setText(this.singleHourPrice);
		this.tfOrderAhead.setText(this.orderAheadHourPrice);
		this.tfCasual.setText(this.casualPrice);
		this.tfFull.setText(this.fullPrice);
		this.tfSiteName.setText("");
	}

	@Override
	public void clearView ()
	{
		this.resetView();

		this.isEditMode = false;
		this.documentState = DocumentState.NOT_EDITED;
		this.updateManageButtons();
		this.currentSiteOnScreen = null;
	}

	@Override
	protected void setupLayout ()
	{
		this.setPreferredSize(new Dimension(908, 704));

		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(10, 60));
		panel.setLayout(new GridLayout(0, 6, 0, 0));
		this.add(panel, BorderLayout.SOUTH);

		this.btnUndo = new JButton(Common.BTN_UNDO);
		this.btnUndo.setName(Common.BTN_UNDO);
		this.btnUndo.setEnabled(false);
		panel.add(this.btnUndo);

		JPanel panel_2 = new JPanel();
		panel.add(panel_2);

		JPanel panel_1 = new JPanel();
		panel.add(panel_1);

		JPanel panel_3 = new JPanel();
		panel.add(panel_3);

		this.btnEdit = new JButton(Common.BTN_EDIT);
		this.btnEdit.setName(Common.BTN_EDIT);
		panel.add(this.btnEdit);

		this.btnSubmit = new JButton(Common.BTN_SUBMIT);
		this.btnSubmit.setEnabled(false);
		this.btnSubmit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnSubmit.setName(Common.BTN_SUBMIT);
		this.btnSubmit.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		panel.add(this.btnSubmit);

		JPanel panel_5 = new JPanel();
		panel_5.setPreferredSize(new Dimension(10, 60));
		this.add(panel_5, BorderLayout.NORTH);
		panel_5.setLayout(new GridLayout(0, 4, 0, 0));

		JPanel panel_4 = new JPanel();
		panel_5.add(panel_4);

		JPanel panel_24 = new JPanel();
		panel_5.add(panel_24);

		JPanel panel_22 = new JPanel();
		panel_5.add(panel_22);

		JPanel panel_23 = new JPanel();
		FlowLayout flowLayout_4 = (FlowLayout) panel_23.getLayout();
		flowLayout_4.setVgap(15);
		flowLayout_4.setAlignment(FlowLayout.RIGHT);
		panel_5.add(panel_23);

		JLabel lblSite = new JLabel("Site:");
		lblSite.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_23.add(lblSite);

		this.tfSiteName = new JTextField();
		this.tfSiteName.setEnabled(false);
		this.tfSiteName.setPreferredSize(new Dimension(100, 28));
		this.tfSiteName.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		this.tfSiteName.setEditable(false);
		panel_23.add(this.tfSiteName);

		JPanel panel_6 = new JPanel();
		panel_6.setPreferredSize(new Dimension(120, 10));
		this.add(panel_6, BorderLayout.WEST);

		JPanel panel_7 = new JPanel();
		panel_7.setPreferredSize(new Dimension(120, 10));
		this.add(panel_7, BorderLayout.EAST);

		JPanel panel_8 = new JPanel();
		this.add(panel_8, BorderLayout.CENTER);
		panel_8.setLayout(new BorderLayout(0, 0));

		JPanel panel_9 = new JPanel();
		panel_9.setPreferredSize(new Dimension(10, 60));
		panel_8.add(panel_9, BorderLayout.SOUTH);

		JPanel panel_10 = new JPanel();
		FlowLayout flowLayout_9 = (FlowLayout) panel_10.getLayout();
		flowLayout_9.setVgap(30);
		flowLayout_9.setAlignment(FlowLayout.LEFT);
		panel_10.setPreferredSize(new Dimension(10, 60));
		panel_8.add(panel_10, BorderLayout.NORTH);

		this.lblUpdateStatus = new JLabel("");
		this.lblUpdateStatus.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_10.add(this.lblUpdateStatus);

		JPanel panel_11 = new JPanel();
		panel_8.add(panel_11, BorderLayout.CENTER);
		panel_11.setLayout(new BorderLayout(0, 0));

		JPanel panel_12 = new JPanel();
		panel_11.add(panel_12, BorderLayout.WEST);
		panel_12.setLayout(new GridLayout(8, 0, 0, 0));

		JPanel panel_13 = new JPanel();
		panel_13.setBorder(new MatteBorder(2, 2, 1, 1, new Color(0, 0, 0)));
		FlowLayout flowLayout = (FlowLayout) panel_13.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		flowLayout.setVgap(15);
		panel_12.add(panel_13);

		JLabel lblNewLabel = new JLabel("Single Use:");
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_13.add(lblNewLabel);

		JPanel panel_14 = new JPanel();
		panel_14.setBorder(new MatteBorder(0, 2, 1, 1, new Color(0, 0, 0)));
		FlowLayout flowLayout_1 = (FlowLayout) panel_14.getLayout();
		flowLayout_1.setAlignment(FlowLayout.RIGHT);
		flowLayout_1.setVgap(15);
		panel_12.add(panel_14);

		JLabel lblOrderAheadUse = new JLabel("Order Ahead Use:");
		lblOrderAheadUse.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_14.add(lblOrderAheadUse);

		JPanel panel_15 = new JPanel();
		panel_15.setBorder(new MatteBorder(0, 2, 1, 1, new Color(0, 0, 0)));
		FlowLayout flowLayout_2 = (FlowLayout) panel_15.getLayout();
		flowLayout_2.setAlignment(FlowLayout.RIGHT);
		flowLayout_2.setVgap(15);
		panel_12.add(panel_15);

		JLabel lblCasualUse = new JLabel("Casual Use:");
		lblCasualUse.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_15.add(lblCasualUse);

		JPanel panel_16 = new JPanel();
		panel_16.setBorder(new MatteBorder(0, 2, 2, 1, new Color(0, 0, 0)));
		FlowLayout flowLayout_3 = (FlowLayout) panel_16.getLayout();
		flowLayout_3.setAlignment(FlowLayout.RIGHT);
		flowLayout_3.setVgap(15);
		panel_12.add(panel_16);

		JLabel lblNewLabel_1 = new JLabel("Full Use:");
		lblNewLabel_1.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_16.add(lblNewLabel_1);

		JPanel panel_17 = new JPanel();
		panel_11.add(panel_17, BorderLayout.CENTER);
		panel_17.setLayout(new GridLayout(8, 0, 0, 0));

		JPanel panel_18 = new JPanel();
		panel_18.setBorder(new MatteBorder(2, 0, 1, 2, new Color(0, 0, 0)));
		FlowLayout flowLayout_5 = (FlowLayout) panel_18.getLayout();
		flowLayout_5.setAlignment(FlowLayout.LEFT);
		flowLayout_5.setVgap(13);
		panel_17.add(panel_18);

		this.tfSingleUse = new JTextField();
		this.tfSingleUse.setEditable(false);
		this.tfSingleUse.setPreferredSize(new Dimension(100, 28));
		this.tfSingleUse.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_18.add(this.tfSingleUse);

		JLabel lblNewLabel_2 = new JLabel("Per Hour");
		panel_18.add(lblNewLabel_2);

		JPanel panel_19 = new JPanel();
		panel_19.setBorder(new MatteBorder(0, 0, 1, 2, new Color(0, 0, 0)));
		FlowLayout flowLayout_6 = (FlowLayout) panel_19.getLayout();
		flowLayout_6.setAlignment(FlowLayout.LEFT);
		flowLayout_6.setVgap(13);
		panel_17.add(panel_19);

		this.tfOrderAhead = new JTextField();
		this.tfOrderAhead.setEditable(false);
		this.tfOrderAhead.setPreferredSize(new Dimension(100, 28));
		this.tfOrderAhead.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_19.add(this.tfOrderAhead);

		JLabel lblPerHour = new JLabel("Per Hour");
		panel_19.add(lblPerHour);

		JPanel panel_20 = new JPanel();
		panel_20.setBorder(new MatteBorder(0, 0, 1, 2, new Color(0, 0, 0)));
		FlowLayout flowLayout_7 = (FlowLayout) panel_20.getLayout();
		flowLayout_7.setAlignment(FlowLayout.LEFT);
		flowLayout_7.setVgap(13);
		panel_17.add(panel_20);

		this.tfCasual = new JTextField();
		this.tfCasual.setEditable(false);
		this.tfCasual.setPreferredSize(new Dimension(100, 28));
		this.tfCasual.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_20.add(this.tfCasual);

		JLabel lblPerMonth = new JLabel("Per Month");
		panel_20.add(lblPerMonth);

		JPanel panel_21 = new JPanel();
		panel_21.setBorder(new MatteBorder(0, 0, 2, 2, new Color(0, 0, 0)));
		FlowLayout flowLayout_8 = (FlowLayout) panel_21.getLayout();
		flowLayout_8.setAlignment(FlowLayout.LEFT);
		flowLayout_8.setVgap(13);
		panel_17.add(panel_21);

		this.tfFull = new JTextField();
		this.tfFull.setEditable(false);
		this.tfFull.setPreferredSize(new Dimension(100, 28));
		this.tfFull.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_21.add(this.tfFull);

		JLabel label = new JLabel("Per Month");
		panel_21.add(label);

	}

	@Override
	protected void setupListeners ()
	{
		this.btnSubmit.addActionListener(this);
		this.btnEdit.addActionListener(this);
		this.btnUndo.addActionListener(this);

		this.tfSingleUse.addKeyListener(this);
		this.tfOrderAhead.addKeyListener(this);
		this.tfCasual.addKeyListener(this);
		this.tfFull.addKeyListener(this);
	}

	@Override
	protected void setupButtonsIcons ()
	{
		int iconTextGap = 20;

		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_SUBMIT, 40, 40);
		if ( icon != null )
		{
			this.btnSubmit.setIcon(icon);
			this.btnSubmit.setIconTextGap(iconTextGap);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_GO_BACK, 40, 40);
		if ( icon != null )
		{
			this.btnUndo.setIcon(icon);
			this.btnUndo.setIconTextGap(iconTextGap);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_EDIT, 40, 40);
		if ( icon != null )
		{
			this.btnEdit.setIcon(icon);
			this.btnEdit.setIconTextGap(iconTextGap);
		}
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		JButton siteCostsButton = (JButton) actionEvent.getSource();
		switch ( siteCostsButton.getName() )
		{
			case Common.BTN_SUBMIT:
				SiteCostUpdateFrom form = null;
				if ( this.isValidPrices() )
				{
					float newSingleUsePrice = Float.parseFloat(this.tfSingleUse.getText());
					float newOrderAheadPrice = Float.parseFloat(this.tfOrderAhead.getText());
					float newCasualPrice = Float.parseFloat(this.tfCasual.getText());
					float newFullPrice = Float.parseFloat(this.tfFull.getText());

					form = new SiteCostUpdateFrom(newSingleUsePrice, newOrderAheadPrice, newCasualPrice, newFullPrice, this.getSelectedSite());
				}

				if ( form != null && this.formSubmitionListener != null )
				{
					this.formSubmitionListener.onFormSubmitted(new FormSubmitionEvent <>(this, form));
					this.isEditMode = false;
					this.documentState = DocumentState.NOT_EDITED;
					this.updateManageButtons();
					this.canEditPrices(false);
				}
				else
				{
					this.controllerDelegate.showPopUpMessage(UserNotification.WRONG_DATA, PopUpMessageType.WARNING);
				}
				break;
			case Common.BTN_EDIT:
				this.isEditMode = true;
				this.updateManageButtons();
				this.canEditPrices(true);
				break;
			case Common.BTN_UNDO:
				this.isEditMode = false;
				this.documentState = DocumentState.NOT_EDITED;
				this.updateManageButtons();
				this.canEditPrices(false);
				this.controllerDelegate.onButtonPress(null);
				break;
		}
	}

	/****************************************************************************************************************************
	 *** KeyListener methods ***
	 ****************************************************************************************************************************/

	@Override
	public void keyReleased ( KeyEvent e )
	{
		if ( this.documentState == DocumentState.NOT_EDITED )
		{
			boolean canChangeState = true;
			String singleUse = this.tfSingleUse.getText();
			if ( this.singleHourPrice.equalsIgnoreCase(singleUse) )
			{
				String orderAheadUse = this.tfOrderAhead.getText();
				if ( this.orderAheadHourPrice.equalsIgnoreCase(orderAheadUse) )
				{
					String casualUse = this.tfCasual.getText();
					if ( this.casualPrice.equalsIgnoreCase(casualUse) )
					{
						String fullUse = this.tfFull.getText();
						canChangeState = ! this.fullPrice.equalsIgnoreCase(fullUse);
					}
				}
			}

			if ( canChangeState )
			{
				this.documentState = DocumentState.EDITED;
				this.updateManageButtons();
			}
		}
	}

	@Override
	public void keyTyped ( KeyEvent e )
	{
	}

	@Override
	public void keyPressed ( KeyEvent e )
	{
	}

}
